using EasyNetQ.Topology;
using RabbitMQ.Client.Exceptions;

namespace EasyNetQ.IntegrationTests.Advanced;

[Collection("RabbitMQ")]
public class When_publish_to_non_existent_exchange : IDisposable
{
    public When_publish_to_non_existent_exchange(RabbitMQFixture rmqFixture)
    {
        bus = RabbitHutch.CreateBus($"host={rmqFixture.Host};prefetchCount=1;timeout=-1");
    }

    public void Dispose() => bus.Dispose();

    private readonly SelfHostedBus bus;

    [Fact]
    public async Task Should_not_affect_publish_to_existent_exchange()
    {
        using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

        await bus.Advanced.ExchangeDeclareAsync("existent", ExchangeType.Topic, cancellationToken: cts.Token);
        await Assert.ThrowsAsync<AlreadyClosedException>(() =>
            bus.Advanced.PublishAsync(
                new Exchange("non-existent"), "#", false, true, MessageProperties.Empty, Array.Empty<byte>(), cts.Token
            )
        );
        await bus.Advanced.PublishAsync(
            new Exchange("existent"), "#", false, true, MessageProperties.Empty, Array.Empty<byte>(), cts.Token
        );
    }
}
